//
//  DZWeichatAuthModel.m
//
//  Created by   on 15/6/2
//  Copyright (c) 2015 __MyCompanyName__. All rights reserved.
//

#import "DZWeichatAuthModel.h"


NSString *const kDZWeichatAuthModelRefreshToken = @"refresh_token";
NSString *const kDZWeichatAuthModelScope = @"scope";
NSString *const kDZWeichatAuthModelUnionid = @"unionid";
NSString *const kDZWeichatAuthModelExpiresIn = @"expires_in";
NSString *const kDZWeichatAuthModelAccessToken = @"access_token";
NSString *const kDZWeichatAuthModelOpenid = @"openid";


@interface DZWeichatAuthModel ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation DZWeichatAuthModel

@synthesize refreshToken = _refreshToken;
@synthesize scope = _scope;
@synthesize unionid = _unionid;
@synthesize expiresIn = _expiresIn;
@synthesize accessToken = _accessToken;
@synthesize openid = _openid;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.refreshToken = [self objectOrNilForKey:kDZWeichatAuthModelRefreshToken fromDictionary:dict];
            self.scope = [self objectOrNilForKey:kDZWeichatAuthModelScope fromDictionary:dict];
            self.unionid = [self objectOrNilForKey:kDZWeichatAuthModelUnionid fromDictionary:dict];
            self.expiresIn = [[self objectOrNilForKey:kDZWeichatAuthModelExpiresIn fromDictionary:dict] doubleValue];
            self.accessToken = [self objectOrNilForKey:kDZWeichatAuthModelAccessToken fromDictionary:dict];
            self.openid = [self objectOrNilForKey:kDZWeichatAuthModelOpenid fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.refreshToken forKey:kDZWeichatAuthModelRefreshToken];
    [mutableDict setValue:self.scope forKey:kDZWeichatAuthModelScope];
    [mutableDict setValue:self.unionid forKey:kDZWeichatAuthModelUnionid];
    [mutableDict setValue:[NSNumber numberWithDouble:self.expiresIn] forKey:kDZWeichatAuthModelExpiresIn];
    [mutableDict setValue:self.accessToken forKey:kDZWeichatAuthModelAccessToken];
    [mutableDict setValue:self.openid forKey:kDZWeichatAuthModelOpenid];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.refreshToken = [aDecoder decodeObjectForKey:kDZWeichatAuthModelRefreshToken];
    self.scope = [aDecoder decodeObjectForKey:kDZWeichatAuthModelScope];
    self.unionid = [aDecoder decodeObjectForKey:kDZWeichatAuthModelUnionid];
    self.expiresIn = [aDecoder decodeDoubleForKey:kDZWeichatAuthModelExpiresIn];
    self.accessToken = [aDecoder decodeObjectForKey:kDZWeichatAuthModelAccessToken];
    self.openid = [aDecoder decodeObjectForKey:kDZWeichatAuthModelOpenid];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_refreshToken forKey:kDZWeichatAuthModelRefreshToken];
    [aCoder encodeObject:_scope forKey:kDZWeichatAuthModelScope];
    [aCoder encodeObject:_unionid forKey:kDZWeichatAuthModelUnionid];
    [aCoder encodeDouble:_expiresIn forKey:kDZWeichatAuthModelExpiresIn];
    [aCoder encodeObject:_accessToken forKey:kDZWeichatAuthModelAccessToken];
    [aCoder encodeObject:_openid forKey:kDZWeichatAuthModelOpenid];
}

- (id)copyWithZone:(NSZone *)zone
{
    DZWeichatAuthModel *copy = [[DZWeichatAuthModel alloc] init];
    
    if (copy) {

        copy.refreshToken = [self.refreshToken copyWithZone:zone];
        copy.scope = [self.scope copyWithZone:zone];
        copy.unionid = [self.unionid copyWithZone:zone];
        copy.expiresIn = self.expiresIn;
        copy.accessToken = [self.accessToken copyWithZone:zone];
        copy.openid = [self.openid copyWithZone:zone];
    }
    
    return copy;
}


@end
